package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import modelo.Item;

import conexao.ConnectionManager;

class ItemDAOImp implements ItemDAO {

	private final String DELETE = "delete from item where idItem = ?";

	private ConnectionManager connectionManager;
	private static Connection connection;

	ItemDAOImp() {
		this.openConnection();
	}

	public boolean isConnectionClose() {
		try {
			return connection.isClosed();
		} catch (SQLException ex) {
			return false;
		}
	}

	public void openConnection() {
		if (connectionManager == null) {
			connectionManager = ConnectionManager.getInstance();
		}

		if (connection == null) {
			connection = connectionManager.getConnection();
		}

		if (isConnectionClose()) {
			connection = connectionManager.getConnection();
		}
	}

	public void closeConnection() {
		if (connection != null && connectionManager != null) {
			if (!this.isConnectionClose()) {
				connectionManager.closeConnection(connection);
			}
		}
	}

	public void deletarItem(Item i) {
		try {
			PreparedStatement ps = null;
			ps = connection.prepareStatement(DELETE);
			// 3 passe para o PreparedStatement o valor do id que deve ser
			// excluido do banco de dados
			ps.setObject(1, i.getIdItem());

			ps.executeUpdate();

		} catch (SQLException ex) {
			Logger.getLogger(ItemDAOImp.class.getName()).log(Level.SEVERE,
					null, ex);
		} finally {
			this.closeConnection();
		}
		
	}

	@Override
	public ItemDAO salvar(ItemDAO e) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void alterar(ItemDAO e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void deletar(ItemDAO e) {
		// TODO Auto-generated method stub

	}

	@Override
	public List<ItemDAO> findAll() {
		// TODO Auto-generated method stub
		return null;
	}


	
}
